﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using TestRunner.Common;
using TestRunner.Events;

namespace TestRunner.ViewModels
{
    public class ToolbarViewModel : ViewModelBase
    {
        #region constructor

        public ToolbarViewModel()
        {
            _fontFamilies = new ObservableCollection<FontFamily>();

            FontFamilies.Add(new FontFamily("Portable User Interface"));
            FontFamilies.Add(new FontFamily("Arial"));
            FontFamilies.Add(new FontFamily("Arial Black"));
            FontFamilies.Add(new FontFamily("Arial Unicode MS"));
            FontFamilies.Add(new FontFamily("Calibri"));
            FontFamilies.Add(new FontFamily("Cambria"));
            FontFamilies.Add(new FontFamily("Cambria Math"));
            FontFamilies.Add(new FontFamily("Comic Sans MS"));
            FontFamilies.Add(new FontFamily("Candara"));
            FontFamilies.Add(new FontFamily("Consolas"));
            FontFamilies.Add(new FontFamily("Constantia"));
            FontFamilies.Add(new FontFamily("Corbel"));
            FontFamilies.Add(new FontFamily("Courier New"));
            FontFamilies.Add(new FontFamily("Georgia"));
            FontFamilies.Add(new FontFamily("Lucida Grande"));
            FontFamilies.Add(new FontFamily("Segoe UI"));
            FontFamilies.Add(new FontFamily("Symbol"));
            FontFamilies.Add(new FontFamily("Tahoma"));
            FontFamilies.Add(new FontFamily("Times New Roman"));
            FontFamilies.Add(new FontFamily("Trebuchet MS"));
            FontFamilies.Add(new FontFamily("Verdana"));

            FontSizes = new ObservableCollection<double?>();
            FontSizes.Add(6);
            FontSizes.Add(8);
            FontSizes.Add(9);
            FontSizes.Add(10);
            FontSizes.Add(11);
            FontSizes.Add(12);
            FontSizes.Add(14);
            FontSizes.Add(18);
            FontSizes.Add(24);
            FontSizes.Add(36);
            FontSizes.Add(48);
            FontSizes.Add(60);
        }

        #endregion constructor

        #region Font Family

        // Conversion not working from TextAligment to Horizontal aligment???
        private ObservableCollection<FontFamily> _fontFamilies;

        public ObservableCollection<FontFamily> FontFamilies
        {
            get { return _fontFamilies; }
        }

        public FontFamily _selectedFontFamily;
        public FontFamily SelectedFontFamily
        {
            get { return _selectedFontFamily; }
            set
            {
                _selectedFontFamily = value;
                ChangeFontFamily();
                RaisePropertyChanged("SelectedFontFamily");
            }

        }


        public void ChangeFontFamily()
        {
            EventManager.FireFontFamilyShapeChanged(this, SelectedFontFamily);               
        }

        #endregion Font Family

        #region Font Sizes


        private ObservableCollection<double?> _fontSizes;

        public ObservableCollection<double?> FontSizes
        {
            get { return _fontSizes; }
            set
            {
                _fontSizes = value;
            }
        }

        public double _selectedFontSize;
        public double SelectedFontSize
        {
            get { return _selectedFontSize; }
            set
            {
                _selectedFontSize = value;
                ChangeFontSize();
                RaisePropertyChanged("SelectedFontSize");
            }

        }

        public void ChangeFontSize()
        {
            EventManager.FireFontSizeShapeChanged(this, SelectedFontSize);            
        }


        #endregion Font Sizes

        #region Fill button / interaction

        private Color _selectedBackgroundColor = Color.FromArgb(255, 84, 141, 212);

        public Color SelectedBackgroundColor
        {
            get { return _selectedBackgroundColor; }
            set
            {
                _selectedBackgroundColor = value;

                // Whenever you choose a color from the color picker popup change as well the
                // backgroiund shape color 
                FillShape();
                RaisePropertyChanged("SelectedBackgroundColor");
            }
        }

        public void FillShape()
        {
            // Apply selected color (change rectangle fill)
            EventManager.FireFillColorShapeChanged(this, SelectedBackgroundColor);
        }


        #endregion Fill button / interaction

        #region Border Color button / interaction

        private Color _selectedBordercolor = Colors.Blue;

        /// <summary>
        /// We associate this to the  pencil button in the toolbar
        /// </summary>
        public Color SelectedBordercolor
        {
            get { return _selectedBordercolor; }
            set
            {
                _selectedBordercolor = value;

                // Whenever you choose a color from the color picker popup change as well the
                // rectangle shape border color 
                ChangeBorderColorShape();

                RaisePropertyChanged("SelectedBorderColor");
            }
        }

        public void ChangeBorderColorShape()
        {
            // Apply selected color (change rectangle border color)
            // Event...
            EventManager.FireBorderColorShapeChanged(this, SelectedBordercolor);            
        }


        #endregion Border Color button / interaction

        #region Font Color button / interaction

        private Color _selectedFontcolor = Colors.Red;

        /// <summary>
        /// We associate this to the  pencil button in the toolbar
        /// </summary>
        public Color SelectedFontColor
        {
            get { return _selectedFontcolor; }
            set
            {
                _selectedFontcolor = value;

                // Whenever you choose a color from the color picker popup change as well the
                // rectangle shape border color 
                ChangeFontColor();

                RaisePropertyChanged("SelectedFontColor");
            }
        }

        /// <summary>
        /// Command when the user clicks on the button, not the dropdown picker we should apply 
        /// the current selected color (send event to the canvas and change the font color of the rectangle text)
        /// </summary>
        public void ChangeFontColor()
        {
            // Notifiy the canvas View of the new selected color
            // if we use prism we could use the eventAggregator
            EventManager.FireFontColorShapeChanged(this, _selectedFontcolor);
        }



        #endregion Font Color button / interaction

        #region Selected Text Aligment button / interaction

        private TextAlignment _selectedTextAligment = TextAlignment.Center;

        public TextAlignment SelectedTextAligment
        {
            get { return _selectedTextAligment; }
            set
            {
                _selectedTextAligment = value;

                ChangeTextAlignment();


                RaisePropertyChanged("SelectedTextAligment");
            }
        }

        public void ChangeTextAlignment()
        {
            HorizontalAlignment selectedHorizAligment = HorizontalAlignment.Left;

            // Whenever you choose a border style change the selected shape border style.
            // If we have this splitted into to different viewmodels we could use some eventing mechanism to communicate the new value
            switch (_selectedTextAligment)
            {
                case TextAlignment.Left:
                    selectedHorizAligment = HorizontalAlignment.Left;
                    break;
                case TextAlignment.Center:
                    selectedHorizAligment = HorizontalAlignment.Center;
                    break;
                case TextAlignment.Right:
                    selectedHorizAligment = HorizontalAlignment.Right;
                    break;

            }

            EventManager.FireSelectedTextAlignment(this, selectedHorizAligment);
        }

        #endregion Selected Text Aligment button / interaction

        #region Selected Font Bold button / interaction

        private Boolean _selectedFontIsBold = false;

        public Boolean SelectedFontIsBold
        {
            get { return _selectedFontIsBold; }
            set
            {
                _selectedFontIsBold = value;

                // Whenever you choose a font to be bold or not update the rectangle shape font weight
                ChangeFontIsBold();


                RaisePropertyChanged("SelectedFontIsBold");
            }
        }

        public void ChangeFontIsBold()
        {
            EventManager.FireSelectedFontBold(this, SelectedFontIsBold);
        }

        #endregion Selected Font Bold button / interaction

        #region Selected Font Italic button / interaction

        private Boolean _selectedFontIsItalic = false;

        public Boolean SelectedFontIsItalic
        {
            get { return _selectedFontIsItalic; }
            set
            {
                _selectedFontIsItalic = value;

                // Whenever you choose a font to be bold or not update the rectangle shape font weight
                ChangeFontIsItalic();


                RaisePropertyChanged("SelectedFontIsItalic");
            }
        }

        public void ChangeFontIsItalic()
        {
            EventManager.FireSelectedFontItalic(this, SelectedFontIsItalic);
        }


        #endregion Selected Font Italic button / interaction

        #region Selected Font Underline button / interaction

        private Boolean _selectedFontIsUnderline = false;

        public Boolean SelectedFontIsUnderline
        {
            get { return _selectedFontIsUnderline; }
            set
            {
                _selectedFontIsUnderline = value;

                // Whenever you choose a font to be bold or not update the rectangle shape font weight                
                ChangeFontIsUnderlined();
                


                RaisePropertyChanged("SelectedFontIsIUnderline");
            }
        }

        public void ChangeFontIsUnderlined()
        {
            EventManager.FireSelectedFontUnderline(this, _selectedFontIsUnderline);
        }


        #endregion Selected Font Underline button / interaction

        #region Selected Border Thickness button / interaction

        private double _selectedBorderThickness = 2;

        public double SelectedBorderThickness
        {
            get { return _selectedBorderThickness; }
            set
            {
                _selectedBorderThickness = value;

                // Whenever you choose a font to be bold or not update the rectangle shape font weight
                ChangeBorderWidthShape();
                //ShapeBorderThickness = _selectedBorderThickness;


                RaisePropertyChanged("SelectedBorderThickness");
            }
        }

        public void ChangeBorderWidthShape()
        {
            EventManager.FireSelectedBorderLineWidth(this, SelectedBorderThickness);
        }

        #endregion Selected Border Thickness button / interaction

        #region Selected Border Style button / interaction

        private DoubleCollection _selectedBorderStyle = null;

        public DoubleCollection SelectedBorderStyle
        {
            get { return _selectedBorderStyle; }
            set
            {
                _selectedBorderStyle = value;

                // Whenever you choose a border style change the selected shape border style.
                // If we have this splitted into to different viewmodels we could use some eventing mechanism to communicate the new value
                ChangeBorderStyleShape();
                


                RaisePropertyChanged("SelectedBorderStyle");
            }
        }

        public void ChangeBorderStyleShape()
        {
            EventManager.FireSelectedBorderLineStyle(this, Extensions.Clone(SelectedBorderStyle));
        }


        #endregion Selected Border Style button / interaction
    }
}
